package com.springboot.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.springboot.demo.common.customValidator.ValidList;
import com.springboot.demo.entity.*;
import com.springboot.demo.entity.validate.DeleteComponentGroup;
import com.springboot.demo.service.PanelService;
import com.springboot.demo.utils.Constant;
import com.springboot.demo.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import javax.validation.groups.Default;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-11-18 14:14
 */
@RestController
@Api(tags = "仪表盘接口")
@Validated
public class PanelController {
    @Autowired
    ResponseUtils responseUtils;
    @Autowired
    PanelService panelService;

    @GetMapping("/operate/panel/all")
    @ApiOperation(value = "获取所有运营仪表盘基本信息", notes = "id，名称等，没有组件信息")
    @ApiOperationSupport(order = 10)
    public ResponseUtils<List<Panel>> getAllOperatePanel() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        // 场景id无意义
        List<Panel> allPanel = panelService.getAllPanel(implUser.getId(), Constant.PanelType.OPERATE_PANEL, 0L);
        return responseUtils.buildSuccessResponse(allPanel);
    }

    @GetMapping("/scene/panel/all")
    @ApiOperation(value = "获取所有场景仪表盘基本信息", notes = "id，名称等，没有组件信息")
    @ApiOperationSupport(order = 15)
    public ResponseUtils<List<Panel>> getAllScenePanel(
            @RequestParam(value = "sceneId")
            @ApiParam(value = "场景id", required = true) Long sceneId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        List<Panel> allPanel = panelService.getAllPanel(implUser.getId(), Constant.PanelType.SCENE_PANEL, sceneId);
        return responseUtils.buildSuccessResponse(allPanel);
    }

    @PostMapping("/operate/panel/name/{name}")
    @ApiOperation(value = "创建运营仪表盘信息", notes = "最多10个,包括默认的")
    @ApiOperationSupport(order = 20)
    public ResponseUtils saveOperatePanel(
            @PathVariable("name") @Length(min = 6, max = 32, message = "名称长度超过限制," +
                    "格式：中文a-zA-Z0-9_-,6~32位")
            @Pattern(regexp = "^[\\u4E00-\\u9FA5a-zA-Z0-9_-]{6,32}$", message = "仪表盘名格式错误") String name) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        // 场景id无意义
        panelService.savePanel(implUser.getId(), Constant.PanelType.OPERATE_PANEL, 0L, name.trim());
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scene/panel/name/{name}")
    @ApiOperation(value = "创建场景仪表盘信息", notes = "最多10个,包括默认的")
    @ApiOperationSupport(order = 25)
    public ResponseUtils saveScenePanel(
            @PathVariable("name") @Length(min = 6, max = 32, message = "名称长度超过限制,格式：中文a-zA-Z0-9_-,6~32位")
            @Pattern(regexp = "^[\\u4E00-\\u9FA5a-zA-Z0-9_-]{6,32}$", message = "仪表盘名格式错误") String name,
            @RequestParam(value = "sceneId")
            @ApiParam(value = "场景id", required = true) Long sceneId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        panelService.savePanel(implUser.getId(), Constant.PanelType.SCENE_PANEL, sceneId, name.trim());
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/panel")
    @ApiOperation(value = "修改仪表盘信息")
    @ApiOperationSupport(order = 30, includeParameters = {"panel.id", "panel.name"})
    public ResponseUtils updatePanel(@Validated @RequestBody Panel panel) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        panelService.updatePanel(implUser.getId(), panel);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/panel/{pid}")
    @ApiOperation(value = "删除仪表盘信息")
    @ApiOperationSupport(order = 40)
    public ResponseUtils deletePanel(
            @PathVariable(value = "pid") @ApiParam(value = "仪表盘id", required = true) Long pid) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        panelService.deletePanel(implUser.getId(), pid);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/panel/{pid}/component")
    @ApiOperation(value = "获取仪表盘组件信息", notes = "报表还需要通过id单独获取表格数据")
    @ApiOperationSupport(order = 50)
    public ResponseUtils<Panel> getPanelComponent(
            @PathVariable(value = "pid") @ApiParam(value = "仪表盘id", required = true) Long pid) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        Panel panelComponent = panelService.getPanelComponent(implUser.getId(), pid);
        return responseUtils.buildSuccessResponse(panelComponent);
    }

    @PostMapping("/panel/component")
    @ApiOperation(value = "为仪表盘增加组件")
    @ApiOperationSupport(order = 60)
    public ResponseUtils savePanelComponent(@Validated({DeleteComponentGroup.class, Default.class})
                                            @RequestBody PanelComponent component) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        panelService.addPanelComponent(implUser.getId(), component);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/panel/component")
    @ApiOperation(value = "为仪表盘修改组件", notes = "主要是保存组件位置信息,用于插入所有组件完保存操作")
    @ApiOperationSupport(order = 70)
    public ResponseUtils updatePanelComponent(@Validated({DeleteComponentGroup.class, Default.class})
                                              @RequestBody ValidList<PanelComponent> componentList) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        panelService.updatePanelComponent(implUser.getId(), componentList);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/panel/component")
    @ApiOperation(value = "为仪表盘删除组件")
    @ApiOperationSupport(order = 80, includeParameters = {"component.componentId", "component.componentType", "component" +
            ".panelId"})
    public ResponseUtils deletePanelComponent(@Validated(DeleteComponentGroup.class) @RequestBody PanelComponent component) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        panelService.deletePanelComponent(implUser.getId(), component);
        return responseUtils.buildSuccessResponse();
    }

}
